using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MindFactorial.LaunchPoint.Console.Library;

namespace MindFactorial.LaunchPoint.Console
{
    /// <summary>
    /// Interaction logic for RegistrationArea.xaml
    /// </summary>

    public partial class RegistrationArea : UserControl
    {
        public event LaunchDelegate _launching;
        string scrollDirection = "";
        double offset = 0;
        double scrollByoffset = 195;
        
        //int waitTime = 10;
        public RegistrationArea()
        {
            InitializeComponent();

            this.upArrow.MouseDown += delegate(object sender, MouseButtonEventArgs e)
            {
                scrollDirection = "Up";
                Scroll();
            };


            this.downArrow.MouseDown += delegate(object sender, MouseButtonEventArgs e)
            {
                scrollDirection = "Down";
                Scroll();
            };
           
        }

    

        void Scroll()
        {
            if (scrollDirection == "Up")
            {
                if (offset <= scrollViewer.ScrollableHeight)
                {
                    offset = offset - scrollByoffset;
                    if (offset < 0)
                    {
                        offset = 0;
                    }
                }
            }
            if (scrollDirection == "Down")
            {
                if (offset >= 0)
                {
                    offset = offset + scrollByoffset;
                    if (offset > scrollViewer.ScrollableHeight)
                    {
                        offset = scrollViewer.ScrollableHeight;
                    }
                }
            }
            this.scrollViewer.ScrollToVerticalOffset(offset);
        }

        internal void RemoveButton(string name)
        {
            Button launchedPlugin = null;
            //find the button based on it's application name
            foreach (FrameworkElement  element in pnlRegistrationButtons.Children)
            {
                if (element.Tag.ToString() == name)
                {
                    launchedPlugin = (Button)element;
                    break;
                }
            }
            pnlRegistrationButtons.Children.Remove(launchedPlugin);

            
        }


        internal void AddButton(string name, string text, bool isactive)
        {
            Button launcher = new Button();
            launcher.Tag = name;
            launcher.Content = text;
            if (isactive)
            {
                launcher.Style = (Style)Resources.MergedDictionaries[0]["PluginActiveButton"];                
            }
            else
            {
                launcher.Style = (Style)Resources.MergedDictionaries[0]["PluginButton"];
            }

            pnlRegistrationButtons.Children.Add(launcher);


            launcher.Click += delegate(object sender, RoutedEventArgs e)
            {
                        
                //
                string appname = name;
                if (_launching != null)
                {
                    _launching(appname);
                }              

            };
        
        }
    }
}